package com.FH.applyForm.officeServies.noticemember;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/26.
 */
@Stateless
public class NoticeMemberDAOImpl extends AccessDB implements NoticeMemberDAO {
    /**
     * 查询发布人员
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getNoticeMemberInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from notice_member where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }
            if (json.has("notice_id")) {
                sql_w += " AND notice_id = '" + json.getString("notice_id") + "'";
            }
            if (json.has("notice_type")) {
                sql_w += " AND notice_type = '" + json.getString("notice_type") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<NoticeMemberShowDTO> result = new ArrayList<NoticeMemberShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select id,member_id,member_name,notice_id,notice_type,isread from notice_member  where 1=1" + sql_w + sql_page;
                seresult = this.getReportingTo(sql);
                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {
                    NoticeMemberShowDTO noticeMemberShowDTO = new NoticeMemberShowDTO();
                    noticeMemberShowDTO.setId(seresult.get(i)[1]);
                    noticeMemberShowDTO.setMember_id(seresult.get(i)[2]);
                    noticeMemberShowDTO.setMember_name(seresult.get(i)[3]);
                    noticeMemberShowDTO.setNotice_id(seresult.get(i)[4]);
                    noticeMemberShowDTO.setNotice_type(seresult.get(i)[5]);
                    noticeMemberShowDTO.setIsread(seresult.get(i)[6]);
                    result.add(noticeMemberShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;

    }

    /**
     * 添加提醒人员集合
     *
     * @param noticeMemberPullDTO
     * @return
     */
    @Override
    public ResultDTO addNoticeMemberInfo(NoticeMemberPullDTO noticeMemberPullDTO) {

        int resultls = 0;
        ResultDTO resultDTO = new ResultDTO();

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");


        String Orgrole_insert = "";
        Orgrole_insert = "insert into notice_member (id,member_id,member_name,notice_id,notice_type,isread)" +
                "values('" + uuid + "','" + noticeMemberPullDTO.getMember_id() + "','" + noticeMemberPullDTO.getMember_name() + "','" + noticeMemberPullDTO.getNotice_id() + "','" + noticeMemberPullDTO.getNotice_type() + "','" + noticeMemberPullDTO.getIsread() + "')";

        resultls = addReportingTo(Orgrole_insert);
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO updateNoticeMemberInfo(NoticeMemberPullDTO noticeMemberPullDTO) {

        ResultDTO resultDTO = new ResultDTO();
        String QUERY = "";
        String sql_w="";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        //更新sql语句
        QUERY += "update notice_member set update_time ='" + time + "'";

        if (null != noticeMemberPullDTO.getIsread() && !"".equals(noticeMemberPullDTO.getIsread())) {
            QUERY += ",isread ='" + noticeMemberPullDTO.getIsread() + "'";
        }
        if (null != noticeMemberPullDTO.getNotice_type() && !"".equals(noticeMemberPullDTO.getNotice_type())) {
            sql_w += " AND notice_type='" + noticeMemberPullDTO.getNotice_type() + "'";
        }
        if (null != noticeMemberPullDTO.getNotice_id() && !"".equals(noticeMemberPullDTO.getNotice_id())) {
            sql_w += " AND notice_id='" + noticeMemberPullDTO.getNotice_id() + "'";
        }
        if (null != noticeMemberPullDTO.getMember_id() && !"".equals(noticeMemberPullDTO.getMember_id())) {
            sql_w += " AND member_id='" + noticeMemberPullDTO.getMember_id() + "'";
        }

        QUERY += " where 1=1 " + sql_w  ;


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除人员集合
     *
     * @param noticeMemberPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteNoticeMemberInfo(NoticeMemberPullDTO noticeMemberPullDTO) {
        String sql = "select count(1) from notice_member WHERE notice_id='" + noticeMemberPullDTO.getNotice_id() + "'";
        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if (!existName.equals("0")) {
            String QUERY = "";
            QUERY += "delete from notice_member  where notice_id='" + noticeMemberPullDTO.getNotice_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
